﻿CREATE FUNCTION PaymentNumberByPrincipal(
	@LoanID int
)RETURNS numeric(5,2) BEGIN
DECLARE @PrincipalBalance money
SELECT @PrincipalBalance = PrincipalBalance FROM PaymentsSummary(@LoanID)
RETURN(
SELECT TOP 1 PaymentNumber+(RemainingPrinciple-@PrincipalBalance)/Payment
FROM PaymentScheduleByLoanID(@LoanID) ORDER BY ABS(RemainingPrinciple-@PrincipalBalance)
)
END
